package tms.controler;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import tms.model.Booking;
import tms.model.Flight;
import tms.service.ServiceHandler;
import tms.view.AddonView;
import tms.view.SummaryPanel;

public class AddonController implements IController {

    private final int SEAT_COLUMNS = 6;

    // which seat has the user chosen?
    private int departSeat = 0;
    private int returnSeat = 0;

    private AddonView view;
    private SummaryController bsc;

    public AddonController(SummaryController bsc) {
        this.bsc = bsc;
    }

    public void createView() {
        view = new AddonView(this);
        view.addSummaryView((SummaryPanel) bsc.getView());

        Booking departTicket = bsc.getDepatureTicket();
        Booking returnTicket = bsc.getReturnTicket();

        // set depart seat assignment board
        Flight departFlight = departTicket.getFlight();
        int departSeats = departFlight.getTotalSeats();
        List<Booking> listBooking =
                ServiceHandler.getListBooking("idflight =" + departFlight.getId());
        view.addDepartSeatAssignment(view.populateSeatBoard(
                departSeats / SEAT_COLUMNS,
                SEAT_COLUMNS,
                getUnavailableSeats(listBooking),
                view.getDepartCheckBoxListener()
        ));

        // set return seat assignment board
        if (returnTicket != null) {
            int returnSeats = 0;
            Flight returnFlight = returnTicket.getFlight();
            returnSeats = returnFlight.getTotalSeats();
            listBooking = ServiceHandler.getListBooking("idflight =" + returnFlight.getId());
            view.addReturnSeatAssignment(view.populateSeatBoard(
                    returnSeats / SEAT_COLUMNS,
                    SEAT_COLUMNS,
                    getUnavailableSeats(listBooking),
                    view.getReturnCheckBoxListener()
            ));
        }
    }

    /**
     * Get a list of unavailable seats from a list of booked tickets.
     * @param listBooking
     * @return an integer list
     */
    public List<Integer> getUnavailableSeats(List<Booking> listBooking) {
        assert (listBooking != null);
        List<Integer> unavailableSeats = new ArrayList<Integer>();
        for (Booking b : listBooking) {
            unavailableSeats.add(b.getSeatNumber());
        }
        return unavailableSeats;
    }

    public void setDepartSeatValue(int value) {
        this.departSeat = value;
    }

    public void setReturnSeatValue(int value) {
        this.returnSeat = value;
    }

    public void moveToConfirmView() {

        Booking departTicket = bsc.getDepatureTicket();
        Booking returnTicket = bsc.getReturnTicket();

        if (departSeat == 0 || (returnTicket != null && returnSeat == 0)) {
            JOptionPane.showMessageDialog(view, "You must choose a seat!");
        } else {
            // update tickets
            assert (departTicket != null);
            departTicket.setSeatNumber(departSeat);
            bsc.setDepatureTicket(departTicket);

            if (returnTicket != null) {
                returnTicket.setSeatNumber(returnSeat);
            }
            bsc.setReturnTicket(returnTicket);

            ConfirmController bcc = new ConfirmController(bsc);
            bcc.createView();
            BaseController.getInstance().changeToPanel((JPanel) bcc.getView());
        }
    }

    public Object getView() {
        if (view == null) {
            createView();
        }
        return view;
    }

}
